#!/bin/bash

#切换到 /root/install 目录
mkdir /root/install/ > /dev/null 2>&1
cd /root/install

# 判断是否初始化
IsInit_ExitScript()
{
    echo "######## Start Install nginx php mariadb nodejs ########"
    
    # 这里需要判断对应的环境是否安装上，只要有一个没安装，则安装需要进行。如果全部安装，则直接停止脚本
}

# 初始化yum仓库
InitYum()
{
    echo "======== Begin InitYum() ========"
    
    # 添加 epel yum仓库
    yum install -y epel-release
    
    # 添加 PHP7 的源
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    
    # 创建 nginx yum仓库配置
    touch /etc/yum.repos.d/nginx.repo
    echo "[nginx]" > /etc/yum.repos.d/nginx.repo
    echo "name=nginx repo" >> /etc/yum.repos.d/nginx.repo
    echo "baseurl=http://nginx.org/packages/centos/7/\$basearch/" >> /etc/yum.repos.d/nginx.repo
    echo "gpgcheck=0" >> /etc/yum.repos.d/nginx.repo
    echo "enabled=1" >> /etc/yum.repos.d/nginx.repo
    
    # 刷新yum缓存
    yum clean all
    yum makecache
    

    echo "======== End InitYum() ========"
}

# 安装所需的软件
YumInstall()
{
    echo "======== Begin YumInstall() ========"
    
    # 安装基本的软件
    yum install -y gcc gcc-c++ wget git net-tools vim
    
    # 安装 nginx mariadb php70 php70-fpm
    yum install -y nginx
    yum install -y mariadb mariadb-server
    yum install -y php70w.x86_64 
    yum install -y php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64
    yum install -y php70w-fpm
    
    echo "======== End YumInstall() ========"
}

# 安装 nodejs （编译安装）
InstallNodejs()
{
    echo "======== Begin InstallNodejs() ========"
    
    # 获取nodejs安装包
    rm -f node-v6.11.3.tar.gz* > /dev/null 2>&1
    wget https://nodejs.org/dist/v6.11.3/node-v6.11.3.tar.gz
    
    # 编译安装
    tar -zxf node-v6.11.3.tar.gz
    cd node-v6.11.3
    ./configure
    make && make install
    
    # 将nodejs相关 命令复制到可搜索目录
    cp -p /usr/local/bin/node /usr/bin
    cp -p /usr/local/bin/node /usr/sbin
    cp -p /usr/local/bin/npm /usr/bin
    cp -p /usr/local/bin/npm /usr/sbin
    
    echo "======== End InstallNodejs() ========"
}

# 安装 thinkjs
InstallThinkjs()
{
    echo "======== Begin InstallThinkjs() ========"
    
    npm install -g think-cli
    
    echo "======== End InstallThinkjs() ========"
}

# nginx 配置
ConfigureNginx()
{
    echo "======== Begin ConfigureNginx() ========"
    mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
    cp $SCRIPT_DIR/file/nginx.conf /etc/nginx/conf.d/
    cp $SCRIPT_DIR/file/1.php /usr/share/nginx/html/
    echo "======== End ConfigureNginx() ========"
}

# 修改其他配置
ConfigureOther()
{
    echo "======== Begin ConfigureOther() ========"
    # 设置防火墙，开放80端口
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --reload

    # 启动并设置开启启动
    systemctl enable nginx
    systemctl enable php-fpm
    systemctl enable mariadb
    systemctl start nginx
    systemctl start php-fpm
    systemctl start mariadb
    echo "======== End ConfigureOther() ========"
}




##### 脚本开始执行 #####
# 初始化参数
SCRIPT_DIR=`pwd`

# 如果已经初始化过了，停止运行
IsInit_ExitScript

# 初始化yum仓库
InitYum

# 安装所需的软件
YumInstall

# 安装 nodejs （编译安装）
#InstallNodejs

# nginx 配置 代理 php-fpm(端口9000) 和 nodejs (默认8360端口)
ConfigureNginx

# 修改其他配置
ConfigureOther


echo "######## Install finished! ########"